package com.example.kt_user.ui

import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.kt_common.base.BaseActivity
import com.example.kt_common.network.BaseStateObserver
import com.example.kt_common.util.Constants
import com.example.kt_common.util.KVUtil
import com.example.kt_common.util.ToastUtil
import com.example.kt_user.viewmodel.LoginViewModel
import com.example.kt_user.R
import com.example.kt_user.bean.request.LoginReq
import com.example.kt_user.databinding.ActivityLoginBinding
import org.koin.androidx.viewmodel.ext.android.viewModel

@Route(path = Constants.PATH_LOGIN)
class LoginActivity : BaseActivity<ActivityLoginBinding>() {

    private val loginViewModel: LoginViewModel by viewModel()

    override fun getLayoutID(): Int {
        return R.layout.activity_login
    }


    override fun init() {
        setDefaulValue()
        mBind.cbRemember.setOnCheckedChangeListener { buttonView, isChecked ->
            if (buttonView.isPressed) {
                KVUtil.put(Constants.isRememberPsw, isChecked)
            }
        }
        loginViewModel.loginData.observe(this, object : BaseStateObserver<String>(null) {
            override fun getRespDataSuccess(it: String) {
                KVUtil.put(Constants.TOKEN, it)
                KVUtil.put(Constants.USER_NAME, mBind.edit1.text.toString())
                KVUtil.put(Constants.PASSWORD, mBind.edit2.text.toString())
                ToastUtil.showMsg("登陆成功！")
                ARouter.getInstance().build(Constants.PATH_MAIN).navigation()
                finish()
            }
        })
        //请求服务器，获取验证码
        loginViewModel.captcha()
        loginViewModel.captchaData.observe(this, object : BaseStateObserver<String>(null) {
            override fun getRespDataSuccess(it: String) {
                mBind.tvCaptcha.text = it
            }

        })

        mBind.txLogin.setOnClickListener {
            //测试
//            ARouter.getInstance().build(Constants.PATH_MAIN).navigation()
            if (mBind.edit1.text.isNotEmpty() && mBind.edit2.text.isNotEmpty() && mBind.etCaptcha.text.isNotEmpty()) {
                // 创建登录请求体
                val loginReq = LoginReq(
                    mBind.edit1.text.toString(),
                    mBind.edit2.text.toString(),
                    mBind.etCaptcha.text.toString()
                )
                loginViewModel.login(loginReq)
            } else {
                ToastUtil.showMsg("输入有误！")
            }
        }

        mBind.tvCaptcha.setOnClickListener {
            //请求服务器，获取验证码
            loginViewModel.captcha()
        }
    }

    private fun setDefaulValue() {
        val isRememberPsw = KVUtil.getBoolean(Constants.isRememberPsw, true)
        if (isRememberPsw){
            mBind.edit1.setText(KVUtil.getString(Constants.USER_NAME))
            mBind.edit2.setText(KVUtil.getString(Constants.PASSWORD))
        }
        mBind.cbRemember.isChecked = isRememberPsw
    }
}


